const Tadpole = require("../../components/js/Tadpole.js");
const Arrow = require("../../components/js/Arrow.js");
const Message = require("../../components/js/Message.js")


let WebSocketService = function(model) {
  let webSocketService = this;
	
	this.hasConnection = false;
	
	this.welcomeHandler = function(data) {
		webSocketService.hasConnection = true;
    let dataId = data.id + "";
    model.myTadpole.id = dataId.substring(2, 9);
    // console.log(data)
    model.tadpoles[dataId.substring(2,9)] = model.tadpoles[-1];
		delete model.tadpoles[-1];
		
		// $('#chat').initChat();
		// if($.cookie('todpole_name'))	{
		// 	webSocketService.sendMessage('name:'+$.cookie('todpole_name'));
		// }
	};
	
	this.updateHandler = function(data) {
		let newtp = false;
    let dataId = data.id + "";
    if (!model.tadpoles[dataId.substring(2, 9)]) {
			newtp = true;
      
      model.tadpoles[dataId.substring(2, 9)] = new Tadpole();
      model.arrows[dataId.substring(2, 9)] = new Arrow(model.tadpoles[dataId.substring(2, 9)], model.camera);
		}
		
    let tadpole = model.tadpoles[dataId.substring(2, 9)];
		
		if(tadpole.id == model.myTadpole.id) {			
			tadpole.name = data.name;
			return;
		} else {
			tadpole.name = data.name;
		}
		
		if(newtp) {
			tadpole.x = data.x;
			tadpole.y = data.y;
		} else {
			tadpole.targetX = data.x;
			tadpole.targetY = data.y;
		}
		
		tadpole.angle = data.angle;
		tadpole.momentum = data.momentum;
    tadpole.size = data.size;
    tadpole.color = data.color;
		
		tadpole.timeSinceLastServerUpdate = 0;
	}
	
	this.messageHandler = function(data) {
    let dataId = data.id + "";
    let tadpole = model.tadpoles[dataId.substring(2, 9)];
		if(!tadpole) {
			return;
		}
		tadpole.timeSinceLastServerUpdate = 0;
		tadpole.messages.push(new Message(data.message));
	}
	
	this.closedHandler = function(data) {
    let dataId = data.id + "";
    if (model.tadpoles[dataId.substring(2, 9)]) {
      delete model.tadpoles[dataId.substring(2, 9)];
      delete model.arrows[dataId.substring(2, 9)];
		}
	}
	
	// this.redirectHandler = function(data) {
	// 	if (data.url) {
	// 		if (authWindow) {
	// 			authWindow.document.location = data.url;
	// 		} else {
	// 			document.location = data.url;
	// 		}			
	// 	}
	// }
	
	this.processMessage = function(data) {
    // console.log(data)
		let fn = webSocketService[data.type + 'Handler'];
		if (fn) {
			fn(data);
		}
	}
	
	this.connectionClosed = function() {
		webSocketService.hasConnection = false;
		// $('#cant-connect').fadeIn(300);
	};
	
	this.sendUpdate = function(tadpole) {
		var sendObj = {
			type: 'update',
			x: tadpole.x.toFixed(1),
			y: tadpole.y.toFixed(1),
			angle: tadpole.angle.toFixed(3),
			momentum: tadpole.momentum.toFixed(3),
      size:tadpole.size,
      color:tadpole.color
		};
		
		if(tadpole.name) {
			sendObj['name'] = tadpole.name;
		}

    wx.sendSocketMessage({
      data: JSON.stringify(sendObj)
    })
		// webSocket.send(JSON.stringify(sendObj));
	}
	
	this.sendMessage = function(msg) {
		// var regexp = /name: ?(.+)/i;
		// if(regexp.test(msg)) {
		// 	model.userTadpole.name = msg.match(regexp)[1];
		// 	$.cookie('todpole_name', model.userTadpole.name, {expires:14});
		// 	return;
		// }
		
		let sendObj = {
			type: 'message',
			message: msg
		};

    wx.sendSocketMessage({
      data: JSON.stringify(sendObj)
    })
		
		// webSocket.send(JSON.stringify(sendObj));
	}
	
	this.authorize = function(token,verifier) {
		var sendObj = {
			type: 'authorize',
			token: token,
			verifier: verifier
		};
    
    wx.sendSocketMessage({
      data: JSON.stringify(sendObj)
    })
		
		// webSocket.send(JSON.stringify(sendObj));
	}
}

module.exports = WebSocketService;